package com.fita.entity.set;

import com.demo2do.core.format.PersistentFormat;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fita.entity.data.Country;
import com.fita.entity.data.Supplier;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 套餐
 * @author mohanlan
 *
 */
@Entity
@Table(name = "pay_set")
@PersistentFormat
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class Set {

  /**
   * 套餐内容键值对
   */
  @Transient
  private static final Map<String, String> contentMap = new HashMap<String, String>();
  
  static {
	  contentMap.put("1", "不限里程");
	  //contentMap.put("2", "碰撞免责条款");
	  contentMap.put("3", "延伸保险");
	  contentMap.put("4", "个人交通意外险/个人损失险");
	  contentMap.put("5", "中文GPS导航");
	  contentMap.put("6", "额外赠送一箱燃油");
	  contentMap.put("7", "税金/一般附加费");
	  contentMap.put("8", "额外4名驾驶员");
	  contentMap.put("9", "Airport Fee");
	  contentMap.put("10", "Theft Protection");
	  contentMap.put("11", "Third Party Liability");
	  contentMap.put("12", "额外2名驾驶员");
	  
  }
  
  /**
   * 套餐包含key
   */
  @Transient
  private static final String[] includeContentKeys = new String[] {"1", "5", "6", "8", "12"};
  
  @Id
  @GeneratedValue
  private Long id;
  
  /** 套餐编码 */
  private String code;
  
  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
  @JsonIdentityReference(alwaysAsId=true)
  private SetCategory categoty;
  
  /**
   * 套餐支付方式
   */
  @Enumerated(EnumType.STRING)
  private SetPayType payType;
  
  /**
   * 套餐所属供应商
   * 
   */
  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
  @JsonIdentityReference(alwaysAsId=true)
  private Supplier supplier;
  
  @Enumerated(EnumType.STRING)
  private SetStatus status;
  
  /**
   * 套餐所属国家
   */
  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
  @JsonIdentityReference(alwaysAsId=true)
  private Country country;
  
  /**
   * 套餐内容
   * 1：不限裡程 
   * 2：碰撞免責條款
   * 3：延伸保險
   * 4：個人交通意外險/個人損失險
   * 5：中文GPS导航
   * 6：額外贈送一箱燃油
   * 7：稅金/一般附加費 
   * 8：額外4名駕駛員 
   * 9：Airport Fee
   * 10：Theft Protection
   * 11：Third Party Liability
   */ 
  private String contents;
  
  @Column(name = "is_disabled")
  private boolean disabled;
  
  /**
   * The default constructor
   */
  public Set() {
	  
  }

  /**
   * 获取内容字符串
   * @return
   */
  @Transient
  public String getContentString() {
	  
	  Map<String, String> contentMap = this.getContentMap();
	  
	  Collection<String> values = contentMap.values();
	  
	  String contentString = StringUtils.join(values, ",");
	  
	  return contentString;
  }
  
  /**
   * 获取套餐包含
   * @return
   */
  @Transient
  public Map<String, String> getIncludeContentMap() {
	  
	  Map<String, String> includeContentMap = new HashMap<String, String>();
	  
	  Map<String, String> contentMap = getContentMap();
	  
	  for (String key : includeContentKeys) {
		
		  String value = contentMap.get(key);
		  
		  if (StringUtils.isNotBlank(value)) {
			  
			  includeContentMap.put(key, value);
		  }
	  }
	  return includeContentMap;
  } 
  
  /**
   * 获取内容Map
   * @return
   */
  @SuppressWarnings("unchecked")
  @Transient
  public Map<String, String> getContentMap() {
	  
	  if (StringUtils.isNoneBlank(contents)) {
		  
		  Map<String, String> contentMap = new HashMap<String, String>();
		  
		  String[] split = this.contents.split(",");
		  
		  for (String key : split) {
			  
			  String value = Set.contentMap.get(key);
			  
			  if (StringUtils.isNotEmpty(value)) {
				  
				  contentMap.put(key, value);
			  }
		  }
		  return contentMap;
	  }
	  return MapUtils.EMPTY_MAP;
  }
  
  /**
   * open
   * @return
   */
  public Set open() {
	  	
	  this.status = SetStatus.OPEN;
	
	  return this;
  }
  
  /**
   * close 
   * @return
   */
  public Set close() {
	  
	  this.status = SetStatus.CLOSE;
	  
	  return this;
  }
  
  /**
   * @return the id
   */
  public Long getId() {
    return id;
  }

  /**
   * @return the code
   */
  public String getCode() {
    return code;
  }

  /**
   * @return the categoty
   */
  public SetCategory getCategoty() {
    return categoty;
  }

  /**
   * @return the payType
   */
  public SetPayType getPayType() {
    return payType;
  }

  /**
   * @return the supplier
   */
  public Supplier getSupplier() {
    return supplier;
  }

  /**
   * @return the status
   */
  public SetStatus getStatus() {
    return status;
  }

  /**
   * @return the country
   */
  public Country getCountry() {
    return country;
  }

  /**
   * @return the contents
   */
  public String getContents() {
    return contents;
  }

  /**
   * @return the disabled
   */
  public boolean isDisabled() {
    return disabled;
  }

  /**
   * @param id the id to set
   */
  public void setId(Long id) {
    this.id = id;
  }

  /**
   * @param code the code to set
   */
  public void setCode(String code) {
    this.code = code;
  }

  /**
   * @param categoty the categoty to set
   */
  public void setCategoty(SetCategory categoty) {
    this.categoty = categoty;
  }

  /**
   * @param payType the payType to set
   */
  public void setPayType(SetPayType payType) {
    this.payType = payType;
  }

  /**
   * @param supplier the supplier to set
   */
  public void setSupplier(Supplier supplier) {
    this.supplier = supplier;
  }

  /**
   * @param status the status to set
   */
  public void setStatus(SetStatus status) {
    this.status = status;
  }

  /**
   * @param country the country to set
   */
  public void setCountry(Country country) {
    this.country = country;
  }

  /**
   * @param contents the contents to set
   */
  public void setContents(String contents) {
    this.contents = contents;
  }

  /**
   * @param disabled the disabled to set
   */
  public void setDisabled(boolean disabled) {
    this.disabled = disabled;
  }

}
